import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import MallWallet from '../views/Mine/Child/mallWallet.vue'
import Empty from '../views/emptyView.vue'
import { KeepAlive } from "vue";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/home/index",
  },
  {
    path: "/home", // 根路由
    meta: {
      title: "首页",
      keepAlive: true,
      showBtmTab: true
    },
    component: Empty,
    redirect: "/home/index",
    children: [
      {
        path: "index",
        name: "home",
        meta: {
          title: "首页",
          showBtmTab: true
        },
        component: () => import("../views/Home/index.vue"),
      },
      {
        path: "productdetail/:id",
        name: "productdetail",
        meta: {
          title: "商品详情",
          showBtmTab: false
        },
        component: () => import("../views/Home/Child/productDetail.vue"),
      },
      {
        path: "qrdd/:id",
        name: "qrdd",
        meta: {
          title: "确定订单",
          showBtmTab: false
        },
        component: () => import("../views/Home/Child/qrdd.vue"),
      },
      {
        path: "prulist/:id",
        name: "prulist",
        meta: {
          title: "确定订单",
          showBtmTab: true
        },
        component: () => import("../views/Home/Child/prulist.vue"),
      },
    ]
  },
  {
    path: "/notice",
    name: "Notice",
    component: () => import("../views/Home/Notice/index.vue"),
    meta: {
      title: "公告",
      keepAlive: false,
      showBtmTab: false
    },
  },
  {
    path: "/regist",
    name: "Regist",
    meta: {
      title: "注册",
      keepAlive: false,
      showBtmTab: false
    },
    component: () => import("../views/Regist/index.vue"),
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录",
      keepAlive: false,
      showBtmTab: false
    },
    component: () => import("../views/Login/index.vue"),
  },
  {
    path: "/login2",
    name: "SMSLogin",
    meta: {
      title: "SMS登录",
      keepAlive: false,
      showBtmTab: false
    },
    component: () => import("../views/Login/index2.vue"),
  },
  {
    path: "/shopping",
    name: "Shopping",
    meta: {
      title: "购物车",
      keepAlive: true,
      showBtmTab: true
    },
    component: () => import("../views/Shopping/index.vue"),
  },
  {
    path: "/apps",
    name: "Classification",
    meta: {
      title: "分类",
      keepAlive: true,
      showBtmTab: true
    },
    component: () => import("../views/Classification/index.vue"),
  },
  {
    path: "/mine",
    meta: {
      title: "我的",
      KeepAlive: true,
      showBtmTab: true
    },
    component: Empty,
    // hide:false,
    redirect: "/mine/index",
    children: [
      {
        path: "index",
        name: "mine",
        meta: {
          title: "我的",
          KeepAlive: true,
          showBtmTab: true
        },
        component: () => import("../views/Mine/index.vue"),
      },
      {
        path: "orderls",
        name: "orderls",
        meta: {
          title: "我的订单",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/myorderls.vue"),
      },
      {
        path: "mallwallet",
        name: "mallWallet",
        meta: {
          title: "商城钱包",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/mallWallet.vue"),
      },
      {
        path: "totalscore",
        name: "totalscore",
        meta: {
          title: "总积分",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/totalScore.vue"),
      },
      {
        path: "rechargepoint",
        name: "rechargepoint",
        meta: {
          title: "充值积分",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/rechargePoint.vue"),
      },
      {
        path: "dynamicwallet",
        name: "dynamicWallet",
        meta: {
          title: "动态钱包",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/dynamicWallet.vue"),
      },
      {
        path: "storagewallet",
        name: "storagewallet",
        meta: {
          title: "储存钱包",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/storageWallet.vue"),
      },
      {
        path: "staticwallet",
        name: "staticWallet",
        meta: {
          title: "静态钱包",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/staticWallet.vue"),
      },
      {
        path: "signIn",
        name: "signIn",
        meta: {
          title: "签到",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/signIn.vue"),
      },
      {
        path: "orderpoint",
        name: "orderPoint",
        meta: {
          title: "报单积分",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/orderPoint.vue"),
      },
      {
        path: "platformintroduction",
        name: "platformintroduction",
        meta: {
          title: "平台介绍",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/platformIntroduction.vue"),
      },
      {
        path: "myteam",
        component: Empty,
        redirect: 'index',
        children: [
          {
            path: "index",
            name: "myteam",
            meta: {
              title: "我的邀请",
              showBtmTab: false
            },
            component: () => import("../views/Mine/Child/myteam.vue"),
          },
          {
            path: "myintiveinfo",
            name: "myintiveinfo",
            meta: {
              title: "我的团队",
              showBtmTab: false
            },
            component: () => import("../views/Mine/Child/myChild/myTeam.vue"),
          }
        ]
      },
      // {
      //   path: "myset",
      //   name: "myset",
      //   meta: {
      //     title: "我的设置",
      //     showBtmTab: false
      //   },
      //   component: () => import("../views/Mine/Child/myset.vue"),
      // },
      {
        path: "myset",
        component: Empty,
        children: [
          {
            path: "",
            name: "myset",
            meta: {
              title: "我的设置",
              showBtmTab: false
            },
            component: () => import("../views/Mine/Child/myset.vue"),
          },
          {
            path: "bindinfo",
            name: "bindinfo",
            meta: {
              title: "绑定信息",
              showBtmTab: false
            },
            component: () => import("../views/Mine/Child/myChild/bindInfo.vue"),
          }, 
          {
            path: "addbankcard",
            name: "addbankcard",
            meta: {
              title: "添加银行卡",
              showBtmTab: false
            },
            component: () => import("../views/Mine/Child/myChild/addBankCard.vue"),
          },
          {
            path: "addbank",
            name: "addbank",
            meta: {
              title: "对公账号",
              showBtmTab: false
            },
            component: () => import("../views/Mine/Child/myChild/corporateAccount.vue"),
          },
          {
            path: "editinfo",
            name: "editinfo",
            meta: {
              title: "修改信息",
              showBtmTab: false
            },
            component: () => import("../views/Mine/Child/myChild/editInfo.vue"),
          },
        ]
      },
      {
        path: "feedback",
        name: "feedback",
        meta: {
          title: "意见反馈",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/feedback.vue"),
      },
      {
        path: "orderInfo",
        name: "orderInfo",
        meta: {
          title: "订单信息",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/ddxx.vue"),
      },

      {
        path: "addressls",
        name: "addressls",
        meta: {
          title: "地址管理",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/addressls.vue"),
      },
      
      {
        path: "tixian/:id",
        name: "tixian",
        meta: {
          title: "提现",
          showBtmTab: false
        },
        component: () => import("../views/Mine/Child/tixian.vue"),
      },
    ]
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes
});
// 路由拦截
router.beforeEach(async (to, from, next) => {
  // 判断用户是否登录
  const token = window.localStorage.getItem("accessToken");
  // 创建一个正则表达式来匹配 /login、/login2 和 /regist 路径
  const loginPathsRegex = /^\/(login|regist|login2)$/;
  if (!token) {
    // console.log("守卫->",loginPathsRegex.test(to.path))
    // 用户未登录
    if (loginPathsRegex.test(to.path)) {
      // 如果用户尝试访问登录或注册页面，允许访问
      next();
    } else {
      // 否则重定向到登录页面
      next('/login');
    }
  } else {
    // 用户已登录
    if (loginPathsRegex.test(to.path)) {
      // 如果用户已登录但尝试访问登录或注册页面，重定向到首页
      next('/');
    } else {
      // 允许访问其他页面
      next();
    }
  }
});
export default router;
